<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BTreeListItem
 *
 * @author jianfeng
 */
class BTreeList extends BEntity {

    protected $owner;
    protected $ownerType;
    protected $ownerId;
    protected $treeItemClass;
    protected $treeItem;
    protected $listNode;
    protected $row;
    public $groupCount;
    public $name;

    public function __construct($treeListNode = NULL, $owner = NULL) {
        if ($treeListNode instanceof BTreeItem) {
            $this->listNode = $treeListNode;
        }

        if ($owner instanceof BEntity) {
            $this->owner = $owner;
            $this->ownerId = $owner->getId();
            $this->ownerType = get_class($owner);
            if ($this->listNode instanceof BTreeItem)
                $this->listNode->setOwner($owner);
        }
    }

    public function getId($data = NULL) {
        $listNode = $this->getListNode();
        if ($listNode instanceof BTreeItem)
            return $listNode->getId();
        return NULL;
    }

    public function getSpaceId($data = NULL) {
        $listNode = $this->getListNode();
        if ($listNode instanceof BTreeItem)
            return $listNode->spaceId;
        return NULL;
    }

    public function getAppId($data = NULL) {
        $listNode = $this->getListNode();
        if ($listNode instanceof BTreeItem)
            return $listNode->appId;
        return NULL;
    }

    public function getName($data = NULL) {
        $listNode = $this->getListNode();
        if ($listNode instanceof BTreeItem)
            return $listNode->getName($data);
        return NULL;
    }

    public function getItem($name) {

        $listNode = $this->getListNode();

        if ($listNode instanceof BTreeItem)
            return $listNode->getChild(array('name' => $name));
    }

    public function addItem($name, $value) {
        $listNode = $this->getListNode();
        if ($listNode instanceof BTreeItem) {
            if (is_numeric($value) || (is_string($value) && strlen($value) < 256)) {
                $item = $listNode->addChild(array('name' => $name, 'value' => $value));
                $item->itemType = 'TreeItem';
                $item->save();
            } else {
                //$data = serialize($value);
                $item = $listNode->addChild(array('name' => $name));
                $item->valueType = 'non-string';
                $item->itemType = 'TreeItem';
                $item->value = $value;
                $item->save();
                return $item;
            }
        }
    }

    public function addItems($itemData = NULL) {

        if (is_array($itemData)) {
            foreach ($itemData as $name => $value) {
                if ($value !== NULL && $value !== '') {
                    $this->addItem($name, $value);
                }
            }
        }
        return $this;

        /**
          if (is_array($itemData) && $itemData != NULL) {
          $insetSql = $this->populateInsertSqlFromArray($itemData);
          $this->getListNode()->getRepository()->executeSql($insetSql);
          }
         * 
         */
        return $this;
    }

    public function getItems($context = NULL) {
        $listNode = $this->getListNode();
        if ($listNode instanceof BTreeItem) {
            return $listNode->getChildren($context);
        }
    }

    public function getItemsInArray($context = NULL) {
        $items = $this->getItems($context);
        $rList = array();
        foreach ($items as $item) {
            $itemName = $item->getName();
            $itemValue = $item->getValue();
            $rList[$itemName] = $itemValue;
        }
        return $rList;
    }

    public function getOwnerId() {
        $listNode = $this->getListNode();
        return $listNode->ownerId;
    }

    public function getOwnerType() {
        $listNode = $this->getListNode();
        return $listNode->ownerType;
    }

    public function getRootNode($context = NULL) {
        if ($this->listNode instanceof BTreeItem)
            return $this->listNode->getRootNode();
    }

    public function getTreeTable($data = NULL) {
        $listNode = $this->getListNode();
        $tableNode = $listNode->getParent();

        return new BTreeTable($tableNode);
    }

    public function getRootNodeContext($context = NULL) {
        if ($this->listNode instanceof BTreeItem)
            return $this->listNode->getRootNodeContext($context);
    }

    public function getListNode($data = NULL) {
        return $this->listNode;
    }

    public function setListNode($listNode) {
        if ($listNode instanceof BTreeItem) {
            $this->listNode = $listNode;
            $this->listNode->itemType = 'TreeList';
            //$this->listNode->save();
        }
    }

    public function save($context = null) {
        $listNode = $this->getListNode();

        if ($this->name != NULL) {
            $listNode->fullName = $listNode->getParent()->fullName . '.' . $this->name;
            $listNode->name = $this->name;
        }

        $listNode->save($context);
    }

    public function setOwner($owner) {
        $listNode = $this->getListNode();
        $listNode->setOwner($owner);
    }

    public function setRow($row) {
        $this->row = $row;
    }

    /**
     * 
     * @param type $data
     * @return BRow
     */
    public function getRow($data = NULL) {
        return $this->row;
    }

    public function populateInsertSqlFromArray($itemData = NULL, $listItem = NULL) {

        if ($listItem == NULL)
            $listItem = $this;

        if ($listItem instanceof BTreeList) {
            $listNode = $listItem->getListNode();
            $parentId = $listNode->getId();
            $parentFullName = $listNode->getFullName();
            $ownerType = $listNode->ownerType;
            $ownerId = $listNode->ownerId;
            $spaceId = $listNode->spaceId;
            $appId = $listNode->appId;
            $tableName = $listNode->getRepository()->getModel()->getTableSchema()->rawName;
        }

        if ($spaceId == NULL) {
            $spaceId = 0;
        }

        if ($appId == NULL) {
            $appId = 0;
        }

        $insertSql = '';
        $insertHeader = 'insert into  ' . $tableName . '(';
        $cols = 'parent_id,owner_type,owner_id,item_type,space_id,app_id,fullname,name,value) VALUES';
        $values = '(' . $parentId . ",'" . $ownerType . "'," . $ownerId . ',' . "'TreeItem'" . ',' . $spaceId . ',' . $appId;
        $insertSql = $insertHeader . $cols;
        $count = count($itemData);
        $i = 0;
        if (is_array($itemData) && $itemData != NULL) {
            foreach ($itemData as $name => $value) {
                $fullName = $parentFullName . '.' . $name;
                //$cols.= ',fullname,name,value';
                $aValues = $values . ",'" . $fullName . "','" . $name . "','" . $value . "'";
                if ($i < $count - 1) {
                    $insertSql.= $aValues . '),';
                } else {
                    $insertSql.= $aValues . ')';
                }
                $i++;
            }
        }

        if ($insertSql != '')
            return $insertSql;

        return FALSE;
    }

}
